<?php

namespace app\admin\controller\disassemble;

use app\admin\model\disassemble\Info;
use app\common\controller\Backend;

/**
 * 回收价格库
 *
 * @icon fa fa-circle-o
 */
class Money extends Backend
{

    /**
     * Money模型对象
     * @var \app\admin\model\disassemble\Money
     */
    protected $model = null;
    /**
     * 是否开启数据限制
     * 支持auth/personal
     * 表示按权限判断/仅限个人
     * 默认为禁用,若启用请务必保证表中存在admin_id字段
     */
    protected $dataLimit = true;

    /**
     * 数据限制字段
     */
    protected $dataLimitField = 'admin_id';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\disassemble\Money;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['price','store'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {

                $row->getRelation('store')->visible(['name']);
                $row->getRelation('price')->visible(['level','weight','end','price','yprice','desc','type','first_id','second_id']);
            }
            foreach ($list as &$value) {
                $value->price->first_id = db('category')->where(['id'=>$value->price->first_id])->value('name');
                $value->price->second_id = db('category')->where(['id'=>$value->price->second_id])->value('name');
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function do_status()
    {
        if($this->request->isPost())
        {
            $param = $this->request->param('row/a');
            $priceids = explode(',',$param['ids']);
            foreach ($priceids as $priceid)
            {
                $back_price = $this->model->find($priceid);
                $param['money'] = bcmul((100 + $param['per'])/100,$back_price['money'],2);
                $back_price->money = $param['money'];
                $back_price->save();
            }
            $this->success();
        }
        $this->view->assign("ids", $this->request->get('ids'));
        return $this->view->fetch();
    }
}
